<route lang="json5" type="page">
{
  style: {
    navigationBarTitleText: '梦瑶诗歌',
    navigationStyle: 'default',
  },
  needLogin: 'true',
}
</route>
<template>
  <wd-toast />

  <view class="container">
    <view class="search-container">
      <wd-search
        v-model="searchKeyword"
        placeholder="搜索专辑名称"
        @search="handleSearch"
        @clear="handleClear"
        shape="round"
        show-cancel
        clear-trigger="always"
      />
    </view>
    <u-scroll-view scroll-y class="list-container">
      <view
        v-for="(item, index) in albumList"
        :key="index"
        class="list-item"
        :style="{ '--index': index }"
        @click="navigateToMedia(item.id)"
      >
        <text class="type-name">{{ index + 1 }} 、{{ item.name }}</text>
      </view>
    </u-scroll-view>
  </view>

  <view class="pagination-container">
    <!-- 分页控件 -->
    <wd-pagination
      v-model="currentPage"
      :page-size="pageSize"
      :total="total"
      @change="handlePageChange"
      show-icon
    />
  </view>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { http } from '@/utils/http'
import type { AlbumView } from '@/api/children.typing'
import { PageResult } from '@/api/poem.typings'
import { useAuthHeaders } from '@/utils/token'
import { useToast } from 'wot-design-uni'

const categoryId = ref('')
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const albumList = ref<AlbumView[]>([])
const loading = ref(false)
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL
const toast = useToast()
const scrollTop = ref(0)
const searchKeyword = ref('')
const activeType = ref<string>('')

onMounted(() => {
  getAlbumList()
})

// 页面初次加载
onLoad((options) => {
  usePageAuth()
  categoryId.value = options.categoryId
  activeType.value = options.type
})

// 搜索处理函数
const handleSearch = () => {
  currentPage.value = 1 // 重置到第一页
  getAlbumList()
}

// 清除搜索
const handleClear = () => {
  searchKeyword.value = ''
  currentPage.value = 1
  getAlbumList()
}

const getAlbumList = async () => {
  if (loading.value) return
  loading.value = true
  try {
    const requestParams = {
      pageNum: currentPage.value,
      pageSize: pageSize.value,
      categoryId: categoryId.value,
      name: searchKeyword.value,
    }
    const res = await http.get<PageResult<AlbumView>>(
      `${API_BASE_URL}/api/v1/album/page`,
      requestParams,
      useAuthHeaders(),
    )

    if (res.code === 200) {
      albumList.value = res.data.list
      total.value = res.data.total
    } else {
      toast.error(`加载专辑列表出错：${res.msg}`)
    }
  } catch (error) {
    toast.error(`加载专辑列表出错：${error}`)
  } finally {
    loading.value = false
  }
}

const handlePageChange = (page) => {
  currentPage.value = page.value
  getAlbumList()
  // scrollToTop()
}

// 滚动到顶部
const scrollToTop = () => {
  scrollTop.value = 0
  setTimeout(() => (scrollTop.value = 0.1), 0)
}

const navigateToMedia = (albumId: number) => {
  uni.navigateTo({
    url: `/pages/poem/children/children-media?albumId=${albumId}&&type=${activeType.value}`,
  })
}
</script>

<style scoped>
.container {
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
  padding: 20rpx;
}

.search-container {
  background: #5b8eff;
  position: sticky;
  top: 0;
  z-index: 100;
}

.list-container {
  height: calc(100vh - 40rpx);
  padding-bottom: 90rpx;
}

.list-item {
  padding: 32rpx 40rpx;
  margin: 20rpx 0;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.9) 0%, rgba(246, 248, 250, 0.9) 100%);
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  position: relative;
  transition: all 0.3s ease;
  box-shadow: 0 8rpx 24rpx rgba(143, 157, 184, 0.12);
}

.list-item::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 8rpx;
  height: 60%;
  background: #5b8eff;
  border-radius: 0 8rpx 8rpx 0;
  transition: all 0.3s ease;
}

.list-item:active {
  transform: scale(0.98);
  box-shadow: 0 4rpx 12rpx rgba(143, 157, 184, 0.2);
}

.list-item:active::before {
  width: 12rpx;
  background: #3a6dd0;
}

.type-name {
  font-size: 34rpx;
  color: #2c3e50;
  font-weight: 500;
  position: relative;
  padding-left: 40rpx;
}

.type-name::before {
  content: '•';
  position: absolute;
  left: 0;
  color: #5b8eff;
  font-size: 48rpx;
  line-height: 1;
  top: -4rpx;
}

/* 自定义滚动条样式 */
::-webkit-scrollbar {
  width: 0;
  background: transparent;
}

/* 入场动画 */
@keyframes itemEntrance {
  from {
    opacity: 0;
    transform: translateY(20rpx);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.list-item {
  animation: itemEntrance 0.4s ease forwards;
  animation-delay: calc(var(--index) * 0.08s);
  opacity: 0;
}

.pagination-container {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(255, 255, 255, 0.9); /* 半透明白色背景 */
  backdrop-filter: blur(5px); /* 背景模糊效果 */
  padding: 20rpx;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.1); /* 上方阴影 */
  z-index: 100; /* 确保在其他元素上方 */
}
</style>
